/**
 * 
 */
package org.hotpotmaterial.codegenn2.utils;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hotpotmaterial.codegenn2.entity.HmteamAccount;
import org.hotpotmaterial.codegenn2.exception.CodeCommonException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author cakydeveloper
 *
 */
public class RequestUtil {

	public static void setResponseHeader(HttpServletResponse response, String fileName) {
		try {
			try {
				fileName = new String(fileName.getBytes(), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				throw new CodeCommonException("IO异常");
			}
			response.setContentType("application/octet-stream;charset=UTF-8");
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			response.addHeader("Pargam", "no-cache");
			response.addHeader("Cache-Control", "no-cache");
		} catch (Exception ex) {
			throw new CodeCommonException("IO异常");
		}
	}

	/**
	 * 获取请求url信息
	 * 
	 * @return
	 */
	public static String getRequestInfo(String reqTag) {
		HttpServletRequest request = getCurrentHttpRequest();
		StringBuilder sb = new StringBuilder();
		if (null != request) {
			// 获取ip
			String ip = request.getHeader("X-Real-IP");
			if (null == ip) {
				// 获取ip地址
				ip = request.getRemoteAddr();
			}
			// 拼接日志
			sb.append("request_url: [").append(request == null ? "" : request.getRequestURL()).append("]")
					.append(", request_method: [").append(request.getMethod()).append("]").append(", request_ip: [")
					.append(ip).append("]").append(", request_user: [").append(getUserInfo(reqTag)).append("]")
					.append(" >>>> ").append(reqTag);
		}
		return sb.toString();
	}

	/**
	 * 获取请求信息
	 * 
	 * @param reqTag
	 * @param url
	 * @param reqParams
	 * @return
	 */
	public static String getRequestInfo(String reqTag, String url, String reqParams) {
		HttpServletRequest request = getCurrentHttpRequest();
		StringBuilder sb = new StringBuilder();
		if (null != request) {
			// 获取ip
			String ip = request.getHeader("X-Real-IP");
			if (null == ip) {
				// 获取ip地址
				ip = request.getRemoteAddr();
			}
			// 拼接日志
			sb.append("request_url: [").append(url).append("]").append(", request_ip: [").append(ip).append("]")
					.append(", request_params: [").append(reqParams).append("]").append(" >>>> ").append(reqTag);
		}
		return sb.toString();
	}

	/**
	 * 获取当前servlet请求
	 * 
	 * @return
	 */
	private static HttpServletRequest getCurrentHttpRequest() {
		// 获取request属性
		HttpServletRequest request = null;
		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		if (requestAttributes != null && requestAttributes instanceof ServletRequestAttributes) {
			request = ((ServletRequestAttributes) requestAttributes).getRequest();
		}
		return request;
	}

	/**
	 * 获取用户名信息
	 * 
	 * @return
	 */
	private static String getUserInfo(String reqTag) {
		// 获取security的context
		SecurityContext securitycontext = SecurityContextHolder.getContext();
		if (null != securitycontext && null != securitycontext.getAuthentication()) {
			Authentication authentication = securitycontext.getAuthentication();
			if (authentication.getPrincipal() instanceof HmteamAccount) {
				HmteamAccount account = (HmteamAccount) authentication.getPrincipal();
				// 设置traceId
				account.setTraceId(reqTag);
				return account.getUserName();
			}
			if (authentication.getPrincipal() == null) {
				return "Anonymous";
			}
			return authentication.getPrincipal().toString();
		}

		return "Anonymous";
	}

	/**
	 * 获取Hmteam对象
	 * 
	 * @return
	 */
	public static HmteamAccount getAccountInfo() {
		// 获取security的context
		SecurityContext securitycontext = SecurityContextHolder.getContext();
		if (null != securitycontext && null != securitycontext.getAuthentication()) {
			Authentication authentication = securitycontext.getAuthentication();
			if (authentication.getPrincipal() instanceof HmteamAccount) {
				HmteamAccount account = (HmteamAccount) authentication.getPrincipal();
				return account;
			}
		}

		return null;
	}

}
